﻿'========================================================================
'功能:發送郵件參數設定,提供維護預計發送的資料,或刪除條件配對的功能
'作者:andy
'日期: 2011/3/25
'========================================================================
Imports System.Data
Partial Class WebForm_Mail_MailSchedule
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            If Not ChkProgramRight(1) Then '查詢
                tcMain.Visible = False
            End If
            If ChkProgramRight(2) Then '新增
                btnSave.Visible = True
                btnNew.Visible = True
            End If
            If ChkProgramRight(3) Then '編輯
                btnSave.Visible = True
            End If


            If Not ChkProgramRight(4) Then '刪除

            End If
            'txtSendDate1.Text = DateTime.Today.ToString("yyyy/MM/dd")
            'txtSendDate2.Text = DateTime.Today.ToString("yyyy/MM/dd")
            ddlDay.Items.Add(New ListItem(GetLocalResourceObject("Day32"), "32"))
            tcMain.ActiveTabIndex = 0


            ddlParamID.DataBind()
            ddlParamID.Items.Insert(0, "")
            ' GetData()
            ' txtSendDate.Text = DateTime.Parse(DateTime.Today.ToString("yyyy/MM/01")).AddMonths(1).AddDays(-1).ToString("yyyy/MM/dd")
        End If

    End Sub
    ''' <summary>
    ''' 取得郵件參設的主旨.內容.回報截止天數及重送天數
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetData(ByVal serno As String)

        Dim bbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()
        Dim dt As DataTable = bbl.GetSendMailbySerno(Integer.Parse(serno), hiDBName.Value)
        If dt.Rows.Count > 0 Then
            ddlParamID.SelectedValue = dt.Rows(0)("ParamID").ToString
            lblSerNo.Text = dt.Rows(0)("SerNo").ToString
            txtMailBody.Value = dt.Rows(0)("MailBody").ToString
            txtMailSubject.Text = dt.Rows(0)("MailSubject").ToString

            txtReturnDays.Text = dt.Rows(0)("ReturnDays").ToString
            txtResendDays.Text = dt.Rows(0)("ResendDays").ToString
            txtResendTimes.Text = dt.Rows(0)("ResendTimes").ToString
            ddlScheduleType.SelectedValue = dt.Rows(0)("ScheduleType").ToString

            txtTopN.Text = dt.Rows(0)("TopN").ToString()
            txtDept.Text = dt.Rows(0)("DeptNo1").ToString()

            txtEquQty.Text = dt.Rows(0)("EquQty").ToString()
            txtSendDate.Text = dt.Rows(0)("SendDate").ToString()
            txtTakeOfficeDate.Text = dt.Rows(0)("TakeOfficeDate").ToString()
            ddlHouseID.SelectedValue = dt.Rows(0)("HouseID").ToString()
            ddlAssetType.SelectedValue = dt.Rows(0)("AssetType").ToString()
            If ddlScheduleType.SelectedValue = "4" Then
                lblDay.Visible = False
                ddlDay.Visible = False
                lblMonth.Visible = False
                ddlMonth.Visible = False
                ddlMonth.Items.Clear()
                ddlMonth.Items.Clear()
                ddlMonth.Items.Add("")
                ddlMonth.Items.Add("1")
                ddlMonth.Items.Add("2")
                ddlMonth.Items.Add("3")
                ddlMonth.Items.Add("4")
                ddlMonth.Items.Add("5")
                ddlMonth.Items.Add("6")
                ddlMonth.Items.Add("7")
                ddlMonth.Items.Add("8")
                ddlMonth.Items.Add("9")
                ddlMonth.Items.Add("10")
                ddlMonth.Items.Add("11")
                ddlMonth.Items.Add("12")

            End If
            If ddlScheduleType.SelectedValue = "1" Then
                lblDay.Visible = True
                ddlDay.Visible = True
                lblMonth.Visible = True
                ddlMonth.Visible = True
                ddlMonth.Items.Clear()
                ddlMonth.Items.Add("")
                ddlMonth.Items.Add("1")
                ddlMonth.Items.Add("2")
                ddlMonth.Items.Add("3")
                ddlMonth.Items.Add("4")
                ddlMonth.Items.Add("5")
                ddlMonth.Items.Add("6")
                ddlMonth.Items.Add("7")
                ddlMonth.Items.Add("8")
                ddlMonth.Items.Add("9")
                ddlMonth.Items.Add("10")
                ddlMonth.Items.Add("11")
                ddlMonth.Items.Add("12")




            End If
            If ddlScheduleType.SelectedValue = "2" Then
                lblDay.Visible = True
                ddlDay.Visible = True
                lblMonth.Visible = True
                ddlMonth.Visible = True
                ddlMonth.Items.Clear()
                ddlMonth.Items.Add("")
                ddlMonth.Items.Add("1")
                ddlMonth.Items.Add("2")
                ddlMonth.Items.Add("3")
            End If
            If ddlScheduleType.SelectedValue = "3" Then
                lblDay.Visible = True
                ddlDay.Visible = True
                lblMonth.Visible = False
                ddlMonth.Visible = False
                ddlMonth.Items.Clear()
                ddlMonth.Items.Add("")
                ddlMonth.Items.Add("1")
                ddlMonth.Items.Add("2")
                ddlMonth.Items.Add("3")
                ddlMonth.Items.Add("4")
                ddlMonth.Items.Add("5")
                ddlMonth.Items.Add("6")
                ddlMonth.Items.Add("7")
                ddlMonth.Items.Add("8")
                ddlMonth.Items.Add("9")
                ddlMonth.Items.Add("10")
                ddlMonth.Items.Add("11")
                ddlMonth.Items.Add("12")

            End If
            ddlMonth.SelectedValue = dt.Rows(0)("Monthst").ToString
            ddlDay.SelectedValue = dt.Rows(0)("Dayst").ToString()
            txtMailBodyChn.Value = dt.Rows(0)("MailBodyChn").ToString
            txtMailsubjectSimplified.Text = dt.Rows(0)("MailSubjectChn").ToString

            txtMailBodyEng.Value = dt.Rows(0)("MailBodyEng").ToString
            txtMailSubjectEng.Text = dt.Rows(0)("MailSubjectEng").ToString


        End If

    End Sub
    Private Sub GetData()

        Dim bbl As AssetManage.BBL.MailParamBBL = New AssetManage.BBL.MailParamBBL()
        Dim dt As DataTable = bbl.GetMailParamByID(ddlParamID.SelectedValue, hiDBName.Value)
        If dt.Rows.Count > 0 Then
            txtMailBody.Value = dt.Rows(0)("MailBody").ToString
            txtMailSubject.Text = dt.Rows(0)("MailSubject").ToString
            txtReturnDays.Text = dt.Rows(0)("ReturnDays").ToString
            txtResendDays.Text = dt.Rows(0)("ResendDays").ToString
            txtResendTimes.Text = dt.Rows(0)("ResendTimes").ToString
            txtMailBodyChn.Value = dt.Rows(0)("MailBodyChn").ToString
            txtMailsubjectSimplified.Text = dt.Rows(0)("MailSubjectChn").ToString

            txtMailBodyEng.Value = dt.Rows(0)("MailBodyEng").ToString
            txtMailSubjectEng.Text = dt.Rows(0)("MailSubjectEng").ToString
        End If
    End Sub


    ''' <summary>
    ''' 發送類別選項改了,相關資料也要自動抓取
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub ddlParamID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlParamID.SelectedIndexChanged
        GetData()
    End Sub

    ''' <summary>
    ''' 存檔
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Not chkSave() Then
            Exit Sub
        End If

        Dim bbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()


        If lblSerNo.Text = "" Then
            bbl.InsertSendMailParam(ddlParamID.SelectedValue, txtMailSubject.Text, txtMailBody.Value, txtReturnDays.Text, txtResendDays.Text, txtResendTimes.Text, hiUID.Value, ddlScheduleType.SelectedValue, txtSendDate.Text, txtTopN.Text, txtDept.Text, "", txtEquQty.Text, ddlAssetType.SelectedValue, txtTakeOfficeDate.Text, ddlHouseID.SelectedValue, "N", ddlMonth.SelectedValue, ddlDay.SelectedValue, txtMailsubjectSimplified.Text, txtMailSubjectEng.Text, txtMailBodyChn.Value, txtMailBodyEng.Value, hiDBName.Value)
        Else
            bbl.UpdateSendMailParam(ddlParamID.SelectedValue, txtMailSubject.Text, txtMailBody.Value, txtReturnDays.Text, txtResendDays.Text, txtResendTimes.Text, hiUID.Value, ddlScheduleType.SelectedValue, txtSendDate.Text, txtTopN.Text, txtDept.Text, "", txtEquQty.Text, ddlAssetType.SelectedValue, txtTakeOfficeDate.Text, ddlHouseID.SelectedValue, "N", ddlMonth.SelectedValue, ddlDay.SelectedValue, lblSerNo.Text, txtMailsubjectSimplified.Text, txtMailSubjectEng.Text, txtMailBodyChn.Value, txtMailBodyEng.Value, hiDBName.Value)
        End If
        '   bbl.InsertSendMailParam(ddlParamID.SelectedValue, txtMailSubject.Text, txtMailBody.Value, txtReturnDays.Text, txtResendDays.Text, txtResendTimes.Text, hiUID.Value, ddlScheduleType.SelectedValue, txtSendDate.Text, txtTopN.Text, ddlDeptID1.SelectedValue, "", txtEquQty.Text, ddlAssetType.SelectedValue, txtTakeOfficeDate.Text, ddlHouseID.SelectedValue, "N", txtMonth.Text, txtDay.Text, hiDBName.Value)
        ShowMessageForAjax(btnSave, GetLocalResourceObject("InsertOK"))
        tcMain.ActiveTabIndex = 0
        gvSendMail.DataBind()
        BlankData()
    End Sub

    ''' <summary>
    ''' 存檔前檢查是否OK
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function chkSave() As Boolean
        If ddlParamID.SelectedValue = "" Then
            ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrParamID"))
            Return False
        End If

        If txtMailSubject.Text.Trim = "" Then
            ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrMailSubject"))
            Return False
        End If
        If txtMailBody.Value.Trim = "" Then
            ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrMailBody"))
            Return False
        End If

        Dim tmp As Integer
        Dim dd As DateTime
        If txtTopN.Text <> "" Then
            Try
                tmp = Integer.Parse(txtTopN.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrTopN"))
                Return False
            End Try
            If tmp < 0 Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrTopN"))
                Return False
            End If
        End If

        If txtEquQty.Text <> "" Then
            Try
                tmp = Integer.Parse(txtEquQty.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrEquQty"))
                Return False
            End Try
            If tmp < 0 Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrEquQty"))
                Return False
            End If
        End If

        If ddlScheduleType.SelectedValue = "4" Then

            Try
                dd = DateTime.Parse(txtSendDate.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrSendDate"))
                Return False
            End Try
            If dd <= Today Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrSendDate"))
                Return False

            End If
        Else
            txtSendDate.Text = ""
        End If




        If (txtTakeOfficeDate.Text <> "") Then
            Try
                dd = DateTime.Parse(txtTakeOfficeDate.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrTakeOfficeDate"))
                Return False
            End Try
            If dd >= Today Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrTakeOfficeDate"))
                Return False
            End If

        End If


        If txtReturnDays.Text <> "" Then
            Try
                tmp = Integer.Parse(txtReturnDays.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrReturnDays"))
                Return False
            End Try
            If tmp < 0 Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrReturnDays"))
                Return False
            End If
        End If

        If txtResendDays.Text <> "" Then
            Try
                tmp = Integer.Parse(txtResendDays.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrResendDays"))
                Return False
            End Try
            If tmp < 0 Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrResendDays"))
                Return False
            End If
        End If
        If txtResendTimes.Text <> "" Then
            Try
                tmp = Integer.Parse(txtResendTimes.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrResendTimes"))
                Return False
            End Try
            If tmp < 0 Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("ErrResendTimes"))
                Return False
            End If
        End If

        If ddlScheduleType.SelectedValue = "1" Then
            If (ddlDay.SelectedValue = "" Or ddlMonth.SelectedValue = "") Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("errScheduleType"))
                Return False
            End If
        End If

        If ddlScheduleType.SelectedValue = "2" Then
            If (ddlDay.SelectedValue = "" Or ddlMonth.SelectedValue = "") Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("errScheduleType"))
                Return False
            End If
        End If
        If ddlScheduleType.SelectedValue = "2" Then
            If ddlMonth.SelectedIndex > 3 Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("errScheduleType"))
                Return False
            End If
        End If
        If ddlScheduleType.SelectedValue = "3" Then
            If (ddlDay.SelectedValue = "") Then
                ShowMessageForAjax(btnSave, GetLocalResourceObject("errScheduleType"))
                Return False
            End If
        End If
        If txtMailBodyChn.Value = "" Then
            ShowMessage(GetLocalResourceObject("errMailBodyChn"))
            Return False
        End If
        If txtMailBodyEng.Value.Trim = "" Then
            ShowMessage(GetLocalResourceObject("errMailBodyEng"))
            Return False
        End If
        If txtMailsubjectSimplified.Text.Trim = "" Then
            ShowMessage(GetLocalResourceObject("errMailSubjectChn"))
            Return False
        End If
        If txtMailSubjectEng.Text.Trim = "" Then
            ShowMessage(GetLocalResourceObject("errMailSubjectEng"))
            Return False
        End If


        Return True
    End Function



    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        'Try
        '    DateTime.Parse(txtSendDate1.Text)
        'Catch ex As Exception
        '    ShowMessageForAjax(txtSendDate1, GetLocalResourceObject("ErrSendDate1"))
        '    gvSendMail.Visible = False
        '    Exit Sub
        'End Try
        'Try
        '    DateTime.Parse(txtSendDate2.Text)
        'Catch ex As Exception
        '    ShowMessageForAjax(txtSendDate1, GetLocalResourceObject("ErrSendDate1"))
        '    gvSendMail.Visible = False
        '    Exit Sub
        'End Try
        gvSendMail.DataBind()

    End Sub

    'Protected Sub gvSendMail_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles gvSendMail.RowDeleted
    '    gvSendMail.DataBind()
    'End Sub

    Protected Sub gvSendMail_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvSendMail.RowCommand
        If e.CommandName = "Edit1" Then
            GetData(e.CommandArgument.ToString())
            tcMain.ActiveTabIndex = 1
        End If
    End Sub

    Protected Sub ddlScheduleType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlScheduleType.SelectedIndexChanged
        If ddlScheduleType.SelectedValue = "4" Then
            lblDay.Visible = False
            ddlDay.Visible = False
            lblMonth.Visible = False
            ddlMonth.Visible = False
            ddlMonth.Items.Clear()
            ddlMonth.Items.Clear()
            ddlMonth.Items.Add("")
            ddlMonth.Items.Add("1")
            ddlMonth.Items.Add("2")
            ddlMonth.Items.Add("3")
            ddlMonth.Items.Add("4")
            ddlMonth.Items.Add("5")
            ddlMonth.Items.Add("6")
            ddlMonth.Items.Add("7")
            ddlMonth.Items.Add("8")
            ddlMonth.Items.Add("9")
            ddlMonth.Items.Add("10")
            ddlMonth.Items.Add("11")
            ddlMonth.Items.Add("12")

        End If
        If ddlScheduleType.SelectedValue = "1" Then
            lblDay.Visible = True
            ddlDay.Visible = True
            lblMonth.Visible = True
            ddlMonth.Visible = True
            ddlMonth.Items.Clear()
            ddlMonth.Items.Add("")
            ddlMonth.Items.Add("1")
            ddlMonth.Items.Add("2")
            ddlMonth.Items.Add("3")
            ddlMonth.Items.Add("4")
            ddlMonth.Items.Add("5")
            ddlMonth.Items.Add("6")
            ddlMonth.Items.Add("7")
            ddlMonth.Items.Add("8")
            ddlMonth.Items.Add("9")
            ddlMonth.Items.Add("10")
            ddlMonth.Items.Add("11")
            ddlMonth.Items.Add("12")




        End If
        If ddlScheduleType.SelectedValue = "2" Then
            lblDay.Visible = True
            ddlDay.Visible = True
            lblMonth.Visible = True
            ddlMonth.Visible = True
            ddlMonth.Items.Clear()
            ddlMonth.Items.Add("")
            ddlMonth.Items.Add("1")
            ddlMonth.Items.Add("2")
            ddlMonth.Items.Add("3")
        End If
        If ddlScheduleType.SelectedValue = "3" Then
            lblDay.Visible = True
            ddlDay.Visible = True
            lblMonth.Visible = False
            ddlMonth.Visible = False
            ddlMonth.Items.Clear()
            ddlMonth.Items.Add("")
            ddlMonth.Items.Add("1")
            ddlMonth.Items.Add("2")
            ddlMonth.Items.Add("3")
            ddlMonth.Items.Add("4")
            ddlMonth.Items.Add("5")
            ddlMonth.Items.Add("6")
            ddlMonth.Items.Add("7")
            ddlMonth.Items.Add("8")
            ddlMonth.Items.Add("9")
            ddlMonth.Items.Add("10")
            ddlMonth.Items.Add("11")
            ddlMonth.Items.Add("12")

        End If

    End Sub

    Protected Sub btnNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNew.Click
        BlankData()
    End Sub
    Private Sub BlankData()
        lblDay.Visible = True
        ddlDay.Visible = True
        lblMonth.Visible = True
        ddlMonth.Visible = True
        ddlMonth.Items.Clear()
        ddlMonth.Items.Add("")
        ddlMonth.Items.Add("1")
        ddlMonth.Items.Add("2")
        ddlMonth.Items.Add("3")
        ddlMonth.Items.Add("4")
        ddlMonth.Items.Add("5")
        ddlMonth.Items.Add("6")
        ddlMonth.Items.Add("7")
        ddlMonth.Items.Add("8")
        ddlMonth.Items.Add("9")
        ddlMonth.Items.Add("10")
        ddlMonth.Items.Add("11")
        ddlMonth.Items.Add("12")

        ddlParamID.SelectedValue = ""
        lblSerNo.Text = ""
        txtMailBody.Value = ""
        txtMailSubject.Text = ""

        txtReturnDays.Text = ""
        txtResendDays.Text = ""
        txtResendTimes.Text = ""
        ddlScheduleType.SelectedValue = "1"

        txtTopN.Text = ""
        txtDept.Text = ""

        txtEquQty.Text = ""
        txtSendDate.Text = ""
        txtTakeOfficeDate.Text = ""
        ddlHouseID.SelectedValue = ""
        ddlAssetType.SelectedValue = ""
        ddlMonth.SelectedValue = ""
        ddlDay.SelectedValue = ""
        txtMailBodyChn.Value = ""
        txtMailBodyEng.Value = ""
        txtMailSubjectEng.Text = ""
        txtMailsubjectSimplified.Text = ""


    End Sub

    Protected Sub btnDept_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDept.Click
        Dim dt As DataTable
        Dim uBBL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        dt = uBBL.GetDept("0", hiDBName.Value)
        PopupFormForAjax(btnDept, "DeptID,DeptName", "DeptID,DeptName", dt, hiDBName.Value, "1", txtDept.ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")
    End Sub

    Protected Sub btnDept0_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDept0.Click
        Dim dt As DataTable
        Dim uBBL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        dt = uBBL.GetDept("0", hiDBName.Value)
        PopupFormForAjax(btnDept, "DeptID,DeptName", "DeptID,DeptName", dt, hiDBName.Value, "1", txtDept0.ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")
    End Sub
End Class
